#include "timesilce.h"

static LIST_HEAD(timeslice_task_list);

void timesLiceExec() {
  ListObj* node;
  TimesilceTaskObj* task;

  list_for_each(node, &timeslice_task_list) {
    task = list_entry(node, TimesilceTaskObj, timeslice_task_list);
    if (task->is_run == TASK_RUN) {
      task->task_hdl();
      task->is_run = TASK_STOP;
    }
  }
}

void timesLiceTick() {
  ListObj* node;
  TimesilceTaskObj* task;

  list_for_each(node, &timeslice_task_list) {
    task = list_entry(node, TimesilceTaskObj, timeslice_task_list);
    if (task->timer != 0) {
      task->timer--;
      if (task->timer == 0) {
        task->is_run = TASK_RUN;
        task->timer = task->timeslice_len;
      }
    }
  }
}

unsigned int timesLiceGetTaskNum() { return list_len(&timeslice_task_list); }

void timesLiceTaskInit(TimesilceTaskObj* obj, void (*task_hdl)(void),
                         unsigned int id, unsigned int timeslice_len) {
  obj->id = id;
  obj->is_run = TASK_STOP;
  obj->task_hdl = task_hdl;
  obj->timer = timeslice_len;
  obj->timeslice_len = timeslice_len;
}

void timesLiceTaskAdd(TimesilceTaskObj* obj) {
  list_insert_before(&timeslice_task_list, &obj->timeslice_task_list);
}

void timesLiceTaskDel(TimesilceTaskObj* obj) {
  if (timesliceTaskIsExist(obj))
    list_remove(&obj->timeslice_task_list);
  else
    return;
}

unsigned char timesliceTaskIsExist(TimesilceTaskObj* obj) {
  unsigned char isexist = 0;
  ListObj* node;
  TimesilceTaskObj* task;

  list_for_each(node, &timeslice_task_list) {
    task = list_entry(node, TimesilceTaskObj, timeslice_task_list);
    if (obj->id == task->id) isexist = 1;
  }

  return isexist;
}

unsigned int timesLiceGetTaskTimesLiceLen(TimesilceTaskObj* obj) {
  return obj->timeslice_len;
}
